10 12 13 4 6 0 -2 rats
Yes.
When the "rats" is encountered,
hasNextInt()
will return false and the loop (and program) will end gracefully.
(The phrase "end gracefully" is what programmers say when they mean that
a program ends as intened, not an unintended crash accompanied by error messages.)
There are several hasNext
methods that are usually used along with
their corresponding next
methods.
The table shows several:
Methods of Scanner | ||
---|---|---|
HasNext Method | Reading Method | |
hasNext() | next() | |
hasNextDouble() | nextDouble() | |
hasNextFloat() | nextFloat() | |
hasNextInt() | nextInt() | |
hasNextLine() | nextLine() | |
hasNextLong() | nextLong() |
Usually your programs will use the methods for int
and double
,
and the hasNext()
and next()
methods.
These last two methods work with tokens which are groups of any characters
delimited (surrounded) by one or more spaces.
Does calling a hasNext()
method consume any data from the file?